package com.wei.duowan.bbs.ui.fragment;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.ForumsBean;
import com.wei.duowan.bbs.model.VariablesBean;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.activity.SearchActivity;
import com.wei.duowan.bbs.ui.adapter.BaseViewPagerAdapter;
import com.wei.duowan.bbs.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：赵若位
 * 时间：2019/8/1 15:11
 * 联系方式：1070138445
 * 功能：首页
 */
public class ForumsFragment extends BaseFragment
{
    public static final String TAG = ForumsFragment.class.getSimpleName();

    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.viewPager)
    ViewPager mViewPager;


    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.fragment_forums;
    }

    @Override
    protected void initView()
    {
        //TODO 虚拟数据初始化
        List<ForumsBean> list = App.getSession().getForumsBeanDao().loadAll();
        if (list == null||list.size() == 0)
        {
            String json = StringUtils.getAssetsJson("论坛.json");
            VariablesBean data = new Gson().fromJson(json, VariablesBean.class);
            list = data.getForums();
            App.getSession().getForumsBeanDao().insertOrReplaceInTx(list);
        }
        list = ForumsBean.getGroupForums();

        ViewPagerAdapter adapter = new ViewPagerAdapter(mActivity.getSupportFragmentManager(), list);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(list.size());
        mTabLayout.setupWithViewPager(mViewPager);
    }


    @OnClick(R.id.tv_search)
    public void onViewClicked()
    {
        mActivity.openActivity(SearchActivity.class);
    }


    private class ViewPagerAdapter extends FragmentStatePagerAdapter
    {
        private List<ForumsBean> mList;


        public ViewPagerAdapter(FragmentManager fm,List<ForumsBean> list)
        {
            super(fm);
            mList = list;
        }

        @Override
        public Fragment getItem(int position)
        {
            return ForumsChildFragment.getFragment(mList.get(position));
        }

        @Override
        public int getCount()
        {
            return mList.size();
        }


        @Nullable
        @Override
        public CharSequence getPageTitle(int position)
        {
            return mList.get(position).getName();
        }
    }



}
